package org.apache.commons.codec.language.bm;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Rule {
    public static final RPattern a = new RPattern() {
        public boolean isMatch(CharSequence charSequence) {
            return true;
        }
    };
    private static final Map b = new EnumMap(d.class);
    private final RPattern c;
    private final String d;
    private final PhonemeExpr e;
    private final RPattern f;

    public interface RPattern {
        boolean isMatch(CharSequence charSequence);
    }

    final class AnonymousClass10 implements RPattern {
        final /* synthetic */ String a;
        final /* synthetic */ boolean b;

        AnonymousClass10(String str, boolean z) {
            this.a = str;
            this.b = z;
        }

        public boolean isMatch(CharSequence charSequence) {
            return charSequence.length() > 0 && Rule.b(this.a, charSequence.charAt(charSequence.length() - 1)) == this.b;
        }
    }

    final class AnonymousClass2 implements RPattern {
        Pattern a = Pattern.compile(this.b);
        final /* synthetic */ String b;

        AnonymousClass2(String str) {
            this.b = str;
        }

        public boolean isMatch(CharSequence charSequence) {
            return this.a.matcher(charSequence).find();
        }
    }

    final class AnonymousClass3 extends Rule {
        final /* synthetic */ int b;
        final /* synthetic */ String c;
        private final int d = this.b;
        private final String e = this.c;

        AnonymousClass3(String str, String str2, String str3, PhonemeExpr phonemeExpr, int i, String str4) {
            this.b = i;
            this.c = str4;
            super(str, str2, str3, phonemeExpr);
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Rule");
            stringBuilder.append("{line=").append(this.d);
            stringBuilder.append(", loc='").append(this.e).append('\'');
            stringBuilder.append('}');
            return stringBuilder.toString();
        }
    }

    final class AnonymousClass5 implements RPattern {
        final /* synthetic */ String a;

        AnonymousClass5(String str) {
            this.a = str;
        }

        public boolean isMatch(CharSequence charSequence) {
            return charSequence.equals(this.a);
        }
    }

    final class AnonymousClass6 implements RPattern {
        final /* synthetic */ String a;

        AnonymousClass6(String str) {
            this.a = str;
        }

        public boolean isMatch(CharSequence charSequence) {
            return Rule.d(charSequence, this.a);
        }
    }

    final class AnonymousClass7 implements RPattern {
        final /* synthetic */ String a;

        AnonymousClass7(String str) {
            this.a = str;
        }

        public boolean isMatch(CharSequence charSequence) {
            return Rule.c(charSequence, this.a);
        }
    }

    final class AnonymousClass8 implements RPattern {
        final /* synthetic */ String a;
        final /* synthetic */ boolean b;

        AnonymousClass8(String str, boolean z) {
            this.a = str;
            this.b = z;
        }

        public boolean isMatch(CharSequence charSequence) {
            return charSequence.length() == 1 && Rule.b(this.a, charSequence.charAt(0)) == this.b;
        }
    }

    final class AnonymousClass9 implements RPattern {
        final /* synthetic */ String a;
        final /* synthetic */ boolean b;

        AnonymousClass9(String str, boolean z) {
            this.a = str;
            this.b = z;
        }

        public boolean isMatch(CharSequence charSequence) {
            return charSequence.length() > 0 && Rule.b(this.a, charSequence.charAt(0)) == this.b;
        }
    }

    public interface PhonemeExpr {
        Iterable getPhonemes();
    }

    static {
        for (d dVar : d.a()) {
            Map enumMap = new EnumMap(g.class);
            for (g gVar : g.a()) {
                Map hashMap = new HashMap();
                for (String str : a.a(dVar).a()) {
                    try {
                        hashMap.put(str, a(b(dVar, gVar, str), a(dVar, gVar, str)));
                    } catch (Throwable e) {
                        throw new IllegalStateException("Problem processing " + a(dVar, gVar, str), e);
                    }
                }
                if (!gVar.equals(g.RULES)) {
                    hashMap.put("common", a(b(dVar, gVar, "common"), a(dVar, gVar, "common")));
                }
                enumMap.put(gVar, Collections.unmodifiableMap(hashMap));
            }
            b.put(dVar, Collections.unmodifiableMap(enumMap));
        }
    }

    public Rule(String str, String str2, String str3, PhonemeExpr phonemeExpr) {
        this.d = str;
        this.c = d(str2 + "$");
        this.f = d("^" + str3);
        this.e = phonemeExpr;
    }

    private static String a(d dVar, g gVar, String str) {
        return String.format("org/apache/commons/codec/language/bm/%s_%s_%s.txt", new Object[]{dVar.b(), gVar.b(), str});
    }

    private static List a(Scanner scanner, String str) {
        List arrayList = new ArrayList();
        int i = 0;
        int i2 = 0;
        while (scanner.hasNextLine()) {
            int i3;
            i2++;
            String nextLine = scanner.nextLine();
            if (i != 0) {
                i3 = nextLine.endsWith("*/") ? 0 : i;
            } else if (nextLine.startsWith("/*")) {
                i3 = 1;
            } else {
                i3 = nextLine.indexOf("//");
                String trim = (i3 >= 0 ? nextLine.substring(0, i3) : nextLine).trim();
                if (trim.length() == 0) {
                    continue;
                } else if (trim.startsWith("#include")) {
                    trim = trim.substring("#include".length()).trim();
                    if (trim.contains(" ")) {
                        System.err.println("Warining: malformed import statement: " + nextLine);
                    } else {
                        arrayList.addAll(a(a(trim), str + "->" + trim));
                    }
                    i3 = i;
                } else {
                    String[] split = trim.split("\\s+");
                    if (split.length != 4) {
                        System.err.println("Warning: malformed rule statement split into " + split.length + " parts: " + nextLine);
                        i3 = i;
                    } else {
                        try {
                            arrayList.add(new AnonymousClass3(e(split[0]), e(split[1]), e(split[2]), c(e(split[3])), i2, str));
                            i3 = i;
                        } catch (Throwable e) {
                            throw new IllegalStateException("Problem parsing line " + i2, e);
                        }
                    }
                }
            }
            i = i3;
        }
        return arrayList;
    }

    private static Scanner a(String str) {
        String format = String.format("org/apache/commons/codec/language/bm/%s.txt", new Object[]{str});
        InputStream resourceAsStream = a.class.getClassLoader().getResourceAsStream(format);
        if (resourceAsStream != null) {
            return new Scanner(resourceAsStream, "UTF-8");
        }
        throw new IllegalArgumentException("Unable to load resource: " + format);
    }

    private static Scanner b(d dVar, g gVar, String str) {
        String a = a(dVar, gVar, str);
        InputStream resourceAsStream = a.class.getClassLoader().getResourceAsStream(a);
        if (resourceAsStream != null) {
            return new Scanner(resourceAsStream, "UTF-8");
        }
        throw new IllegalArgumentException("Unable to load resource: " + a);
    }

    private static e b(String str) {
        int indexOf = str.indexOf("[");
        if (indexOf < 0) {
            return new e(str, a.b);
        }
        if (str.endsWith("]")) {
            return new e(str.substring(0, indexOf), b.a(new HashSet(Arrays.asList(str.substring(indexOf + 1, str.length() - 1).split("[+]")))));
        }
        throw new IllegalArgumentException("Phoneme expression contains a '[' but does not end in ']'");
    }

    private static boolean b(CharSequence charSequence, char c) {
        for (int i = 0; i < charSequence.length(); i++) {
            if (charSequence.charAt(i) == c) {
                return true;
            }
        }
        return false;
    }

    private static PhonemeExpr c(String str) {
        if (!str.startsWith("(")) {
            return b(str);
        }
        if (str.endsWith(")")) {
            List arrayList = new ArrayList();
            String substring = str.substring(1, str.length() - 1);
            for (String b : substring.split("[|]")) {
                arrayList.add(b(b));
            }
            if (substring.startsWith("|") || substring.endsWith("|")) {
                arrayList.add(new e("", a.b));
            }
            return new f(arrayList);
        }
        throw new IllegalArgumentException("Phoneme starts with '(' so must end with ')'");
    }

    private static boolean c(CharSequence charSequence, CharSequence charSequence2) {
        if (charSequence2.length() > charSequence.length()) {
            return false;
        }
        int length = charSequence.length() - 1;
        for (int length2 = charSequence2.length() - 1; length2 >= 0; length2--) {
            if (charSequence.charAt(length) != charSequence2.charAt(length2)) {
                return false;
            }
            length--;
        }
        return true;
    }

    private static RPattern d(String str) {
        boolean z = true;
        boolean startsWith = str.startsWith("^");
        boolean endsWith = str.endsWith("$");
        String substring = str.substring(startsWith ? 1 : 0, endsWith ? str.length() - 1 : str.length());
        if (substring.contains("[")) {
            boolean startsWith2 = substring.startsWith("[");
            boolean endsWith2 = substring.endsWith("]");
            if (startsWith2 && endsWith2) {
                String substring2 = substring.substring(1, substring.length() - 1);
                if (!substring2.contains("[")) {
                    endsWith2 = substring2.startsWith("^");
                    substring = endsWith2 ? substring2.substring(1) : substring2;
                    if (endsWith2) {
                        z = false;
                    }
                    if (startsWith && endsWith) {
                        return new AnonymousClass8(substring, z);
                    }
                    if (startsWith) {
                        return new AnonymousClass9(substring, z);
                    }
                    if (endsWith) {
                        return new AnonymousClass10(substring, z);
                    }
                }
            }
        } else if (startsWith && endsWith) {
            return substring.length() == 0 ? new RPattern() {
                public boolean isMatch(CharSequence charSequence) {
                    return charSequence.length() == 0;
                }
            } : new AnonymousClass5(substring);
        } else {
            if ((startsWith || endsWith) && substring.length() == 0) {
                return a;
            }
            if (startsWith) {
                return new AnonymousClass6(substring);
            }
            if (endsWith) {
                return new AnonymousClass7(substring);
            }
        }
        return new AnonymousClass2(str);
    }

    private static boolean d(CharSequence charSequence, CharSequence charSequence2) {
        if (charSequence2.length() > charSequence.length()) {
            return false;
        }
        for (int i = 0; i < charSequence2.length(); i++) {
            if (charSequence.charAt(i) != charSequence2.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    private static String e(String str) {
        if (str.startsWith("\"")) {
            str = str.substring(1);
        }
        return str.endsWith("\"") ? str.substring(0, str.length() - 1) : str;
    }
}
